package com.hbhmzy.contact;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ContactDBManager {

	/**
	 * 插入 
	 * @param db
	 * @param name
	 * @param number
	 * @param sortkey
	 * @param email
	 * @param address
	 */
	public static void insert(SQLiteDatabase db, String name, String number,String sortkey, String email, String address) {
		
		db.execSQL("insert into contact_info values(null,?,?,?,?,?)", new String[] { name, number, sortkey, email, address});
		db.close();
		Log.e("insert success", "insert into contact_info values(" + name + "," + number + "," + sortkey  + "," + email + "," + address + ")");
	}
	
	/** 根据姓名和号码删除数据库中对应的记录
	 * @param db
	 * @param name
	 * @param number
	 */
	public static void deleteByNameNum(SQLiteDatabase db, String name, String number){
    	db.execSQL("delete from contact_info where name=? AND number=?", new String[]{name, number});
    	db.close();
    	Log.e("---delete success---", "delete from contact_info where name=" + name + ",number=" + number);
	}
	
	/** 根据姓名、号码、sortkey更新数据库中对应的记录
	 * @param db
	 * @param name
	 * @param number
	 * @param newName
	 * @param newNumber
	 * @param newSortkey
	 */
	public static void update(SQLiteDatabase db, String name, String number,
			String newName, String newNumber, String newSortkey){
		/*String sql = "UPDATE contact_info SET name = newName, number = newNumber "
				+ "WHERE LastName = 'Wilson'";
    	db.execSQL(sql);*/
		
		ContentValues values = new ContentValues();
		// 存放更新后的姓名和号码
		values.put("name", newName);
		values.put("number", newNumber);
		values.put("sortkey", newSortkey);
		// 返回受此 update 语句影响的记录的条数
		int result = db.update("contact_info", values, "name=? AND number=?", new String[]{name, number});
    	db.close();
    	Log.e("---update success---", "result=" + result + " :" +name + number + "-->" + newName + newNumber);
	}

}
